import pymysql
from pyspark.context import SparkContext
import time

sc = SparkContext()

lines_rdd = sc.textFile("../../data/students.txt")

# start = time.time()
# # 1、创建数据库链接
# # 不能在Driver端创建数据库链接，在Executor端使用
# # 因为数据库链接不能序列化
# # 网络链接不能在网络中传输
# con = pymysql.connect(host="master", port=3306, user="root", password="123456", database="myemployees")
# end = time.time()
# print(end - start)

def to_mysql(line):
    split = line.split(",")
    id = split[0]
    name = split[1]
    age = int(split[2])
    sex = split[3]
    clazz = split[4]

    start = time.time()
    # 1、创建数据库链接
    con = pymysql.connect(host="master", port=3306, user="root", password="123456", database="myemployees")
    end = time.time()
    print(end - start)

    # 2、获取游标
    cursor = con.cursor()

    # 3、执行sql
    cursor.execute("insert into students(id,name,age,sex,clazz) values(%s,%s,%s,%s,%s)", (id, name, age, sex, clazz))
    # 提交事务
    con.commit()



# 循环将数据写入mysql
lines_rdd.foreach(to_mysql)
